package quartz.quartzdemo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import quartz.quartzdemo.pojo.QuartzEntity;
import quartz.quartzdemo.service.JobService;

import java.util.logging.SimpleFormatter;

@RestController
public class JobController {
    @Autowired
    private JobService jobService;

    @GetMapping("addJob")
    public Boolean addJob() {
        QuartzEntity quartz = new QuartzEntity();

        quartz.setJobName("启动" + 1);
        quartz.setJobGroup("计划任务");
        quartz.setJobClassName("quartz.quartzdemo.task.JobExe");//具体的job
        // 类，真正需要执行的代码
        quartz.setDescription("计划任务启动:" + "taskname");
        SimpleFormatter format1 = new SimpleFormatter();
        //quartz.setCronExpression(format1.format(new Date()));//"ss mm HH dd MM ? //yyyy"  根据前台传递的参数，到点执行
        quartz.setCronExpression("*/20 * * * * ? ");
       // JSONObject params = JSON.parseObject("yes");
        // Object params = JSON.toJSON("yes");
        jobService.save(quartz, "yes");
        return true;
    }

    @GetMapping("updateJob")
    public Boolean updateJob(String name,String group) {
        QuartzEntity quartz = new QuartzEntity();
        quartz.setOldJobGroup(group);
        quartz.setOldJobName(name);
        quartz.setJobName("启动" + 1);
        quartz.setJobGroup("计划任务");
        quartz.setJobClassName("quartz.quartzdemo.task.JobExe");//具体的job
        // 类，真正需要执行的代码
        quartz.setDescription("计划任务启动:" + "taskname");
        SimpleFormatter format1 = new SimpleFormatter();
        //quartz.setCronExpression(format1.format(new Date()));//"ss mm HH dd MM ? //yyyy"  根据前台传递的参数，到点执行
        quartz.setCronExpression("*/20 * * * * ? ");
        // JSONObject params = JSON.parseObject("yes");
        // Object params = JSON.toJSON("yes");
        jobService.save(quartz, "yes");
        return true;
    }
}
